package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.generateRFC;

import java.sql.Date;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JLabel;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.Infrastructure;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.RFC;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.Service;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.User;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.generateRFCPanel.generateRFCSubs.DatainputPanel;

public class GenerateNewRFCThread extends Thread
{	
	private DatainputPanel datainputPanel;
	private JLabel progressbar;
	private DatabaseConnection connection;
	private String userName;
	public GenerateNewRFCThread(DatainputPanel datainputPanel, JLabel jLabel)
	{		
		this.datainputPanel = datainputPanel;
		this.progressbar = jLabel;
		this.connection = new DatabaseConnection();
	}

	public void run()
	{
		createNewRFC();
	}

	private void createNewRFC()
	{
		this.connection.connect();		
		this.datainputPanel.setupInputFields();
		
		String user = datainputPanel.getUser().getFirstname()+" "+datainputPanel.getUser().getName();
		this.userName = user; 
		
		String query = "INSERT INTO CMS_RFC(" +
		RFC.TC_SUBMISSIONDATE+", "+		
		RFC.TC_CHANGEOWNER+", "+		
		RFC.TC_RFCINITIATOR+", "+		
		RFC.TC_CHANCHAPPROVER+", "+		
		RFC.TC_PRIORITY+", "+		
		RFC.TC_SUMMARY+", "+		
		RFC.TC_DETAILEDDESCRIPTION+", "+		
		RFC.TC_REASONFORCHANGE+", "+		
		RFC.TC_COSTS+", "+		
		RFC.TC_BENEFITS+", "+		
		RFC.TC_RISKS+", "+		
		RFC.TC_AFFECTEDSERVICES+", "+		
		RFC.TC_AFFECTEDINFRASTRUCTURE+", "+		
		RFC.TC_DATEOFREJECTIONORAPPROVAL+", "+		
		RFC.TC_APPROVALPERSON+", "+		
		RFC.TC_REASONOFREJECTING+","+		
		RFC.TC_Status+		
		") VALUES (CURDATE(),'','"+user+"','','','','','','','','','','',CURDATE(),'','','unfinished')";
		
		try
		{
			this.connection.insertquery(query);			
		}
		catch(Exception e)
		{
			System.out.println("Insert falils");
		}
		
		this.generateKnownValues();	
		this.progressbar.setVisible(false);
		this.connection.disconnect();
	}

	private void generateKnownValues()
	{
		String query = "SELECT MAX("+RFC.TC_ID+") FROM CMS_RFC";
		
		ResultSet rs = connection.query(query);
		int id;
		
		try
		{
			while(rs.next())
			{
				id = rs.getInt(1);
				this.datainputPanel.getLb_IDValue().setText("" + id);
			}
		}
		catch(Exception e)
		{}
		
		query = "SELECT * FROM CMS_user WHERE `"+User.getTC_FUNKTION()+"` = 'usrCO'";		
		ResultSet rs2 = connection.query(query);		
		ArrayList<String> usrSAList = new ArrayList<String>();
		try
		{
			while(rs2.next())
			{
				String usrs = rs2.getString(User.getTC_FIRSTNAMENAME())+ " "+rs2.getString(User.getTC_NAME());
				usrSAList.add(usrs);
			}
		}
		catch(Exception e)
		{}
		
		String[] usrSA = new String[usrSAList.size()];

		this.datainputPanel.getCb_makerValue().removeAllItems();
		
		for(String string : usrSAList)
		{
			this.datainputPanel.getCb_makerValue().addItem(string);
		}
						
		query = "SELECT * FROM "+Infrastructure.TABLENAME;		
		ResultSet rs3 = connection.query(query);		
		ArrayList<String> infrstructure = new ArrayList<String>();
		try
		{
			while(rs3.next())
			{
				infrstructure.add(rs3.getString(Infrastructure.TC_NAME));
			}
		}
		catch(Exception e)
		{}
		
	//	String[] infr = new String[infrstructure.size()];
		
		this.datainputPanel.getCb_infra().removeAllItems();
		
		for(String string : infrstructure)
		{
			this.datainputPanel.getCb_infra().addItem(string);
		}
		
		query = "SELECT * FROM "+ Service.TABLENAME;		
		ResultSet rs4 = connection.query(query);		
		ArrayList<String> serv = new ArrayList<String>();
		try
		{
			while(rs4.next())
			{
				serv.add(rs4.getString(Service.TC_NAME));
			}
		}
		catch(Exception e)
		{}
		
//		String[] servs = new String[serv.size()];
		
		this.datainputPanel.getCb_services().removeAllItems();
		
		for(String string : serv)
		{
			this.datainputPanel.getCb_services().addItem(string);
		}
		
		this.datainputPanel.setUsrSA(usrSA);
		this.datainputPanel.getLb_generaterValue().setText(userName);
		SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd 'um' HH:mm:ss");
		Date date  = new Date(System.currentTimeMillis());
		this.datainputPanel.getLb_dateValue().setText(""+format.format(date));
	}
}